package com.boot.main.config.configDemo;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootConfiguration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
        corsRegistry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("http://127.0.0.1:8090")
                .allowedMethods("POST","GET","PUT","DELETE")
                .allowedHeaders("*")
                .maxAge(1800);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        String path = System.getProperty("user.dir") + "\\ccom-admin-main\\src\\main\\resources\\static\\images\\";
        String path1 = System.getProperty("user.dir") + "\\ccom-admin-main\\src\\main\\resources\\static\\upload\\";
        // 这里是访问地址匹配，只要匹配到字段的话，就当静态资源映射下
        registry.addResourceHandler("/images/**").addResourceLocations("file:"+path);
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+path1);
    }
}
